package com.roamer.javase.demo.export.vo;

import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 多部分数据
 *
 * @author roamer
 * @version v1.0
 * @date 2020/5/5 17:25
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class MultipartModel extends ExportModel {
    private static final long serialVersionUID = -2799134454672708920L;

    private ExportModel exportModel1;

    private ExportModel exportModel2;

    public MultipartModel(ExportModel exportModel1, ExportModel exportModel2) {
        assert exportModel1 != null && exportModel2 != null;
        assert exportModel1.getDataItems() != null && exportModel2.getDataItems() != null;
        this.exportModel1 = exportModel1;
        this.exportModel2 = exportModel2;
    }

    @Override
    public List<Map<String, Object>> getDataMapList() {
        List<Map<String, Object>> dataMapList1 = exportModel1.getDataMapList();
        List<Map<String, Object>> dataMapList2 = exportModel2.getDataMapList();
        int length = Math.min(dataMapList1.size(), dataMapList2.size());
        int max = Math.min(dataMapList1.size(), dataMapList2.size());
        List<Map<String, Object>> list = new ArrayList<>(max);
        // 合并结果集
        for (int i = 0; i < length; i++) {
            Map<String, Object> map = new LinkedHashMap<>(dataMapList1.get(i));
            map.put("", "");
            dataMapList2.get(i).forEach((k, v) -> map.put(k + "_1", v));
            list.add(map);
        }
        // 补多余位置
        for (int i = length; i < dataMapList1.size(); i++) {
            Map<String, Object> map = new LinkedHashMap<>(dataMapList1.get(i));
            map.put("", "");
            // 补空
            exportModel2.getExportHeaderAlias().forEach((k, v) -> map.put(k + "_1", ""));
            list.add(map);
        }
        for (int i = length; i < dataMapList2.size(); i++) {
            Map<String, Object> map = new LinkedHashMap<>();
            // 补空
            exportModel1.getExportHeaderAlias().forEach((k, v) -> map.put(k, ""));
            map.put("", "");
            dataMapList2.get(i).forEach((k, v) -> map.put(k + "_1", v));
            list.add(map);
        }
        return list;
    }

    @Override
    public List<int[]> getMergedRegion() {
        List<int[]> list = new ArrayList<>();
        // 寻找最大的记录结束行
        int max = Math.max(
                exportModel1.getMergedRegion().stream().mapToInt(ints -> ints[1]).max().getAsInt(),
                exportModel2.getMergedRegion().stream().mapToInt(ints -> ints[1]).max().getAsInt());
        // 处理第一个模组
        for (int[] regions : exportModel1.getMergedRegion()) {
            list.add(new int[]{regions[0], max, regions[2], regions[3]});
        }
        // 处理下一个模型的开始列坐标
        int offset = exportModel1.getExportHeaderAlias().size();
        // 空白列
        list.add(new int[]{1, max, offset, offset});
        // 第二个模组合并
        for (int[] regions : exportModel2.getMergedRegion()) {
            list.add(new int[]{regions[0], max, regions[2] + offset + 1, regions[3] + offset + 1});
        }
        return list;
    }

    @Override
    public String getExportTitle() {
         return exportModel1.getExportTitle();
    }

    @Override
    public Map<String, String> getExportHeaderAlias() {
        Map<String, String> headerAlias = new LinkedHashMap<>(exportModel1.getExportHeaderAlias());
        headerAlias.put("", "");
        exportModel2.getExportHeaderAlias().forEach((k, v) -> headerAlias.put(k + "_1", v + " "));
        return headerAlias;
    }
}
